package com.mathlearning.auth.controller;

import com.mathlearning.auth.common.ApiResponse;
import com.mathlearning.auth.entity.*;
import com.mathlearning.auth.service.HomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/home")
public class HomeController {

    @Autowired
    private HomeService homeService;

    /**
     * 获取轮播图列表
     */
    @GetMapping("/banners")
    public ApiResponse<List<Banner>> getBanners() {
        List<Banner> banners = homeService.getBanners();
        return ApiResponse.success(banners);
    }

    /**
     * 获取学习计划列表
     */
    @GetMapping("/learning-plans")
    public ApiResponse<List<LearningPlan>> getLearningPlans() {
        List<LearningPlan> plans = homeService.getLearningPlans();
        return ApiResponse.success(plans);
    }

    /**
     * 获取学习主题列表
     */
    @GetMapping("/learning-topics")
    public ApiResponse<List<LearningTopic>> getLearningTopics(
            @RequestParam(required = false) String category) {
        List<LearningTopic> topics;
        if (category != null && !category.isEmpty()) {
            topics = homeService.getLearningTopicsByCategory(category);
        } else {
            topics = homeService.getLearningTopics();
        }
        return ApiResponse.success(topics);
    }

    /**
     * 获取特色课程列表
     */
    @GetMapping("/featured-courses")
    public ApiResponse<List<Course>> getFeaturedCourses() {
        List<Course> courses = homeService.getFeaturedCourses();
        return ApiResponse.success(courses);
    }

    /**
     * 获取所有课程列表
     */
    @GetMapping("/courses")
    public ApiResponse<List<Course>> getCourses() {
        List<Course> courses = homeService.getCourses();
        return ApiResponse.success(courses);
    }

    /**
     * 获取今日每日一题
     */
    @GetMapping("/daily-problem")
    public ApiResponse<DailyProblem> getTodayDailyProblem() {
        DailyProblem problem = homeService.getTodayDailyProblem();
        return ApiResponse.success(problem);
    }

    /**
     * 获取用户学习统计
     */
    @GetMapping("/user-stats")
    public ApiResponse<UserStats> getUserStats(@RequestParam Long userId) {
        UserStats stats = homeService.getUserStats(userId);
        return ApiResponse.success(stats);
    }

    /**
     * 获取首页所有数据（综合接口）
     */
    @GetMapping("/all")
    public ApiResponse<Map<String, Object>> getHomeData() {
        Map<String, Object> data = new HashMap<>();
        
        data.put("banners", homeService.getBanners());
        data.put("learningPlans", homeService.getLearningPlans());
        data.put("learningTopics", homeService.getLearningTopics());
        data.put("featuredCourses", homeService.getFeaturedCourses());
        data.put("dailyProblem", homeService.getTodayDailyProblem());
        
        return ApiResponse.success(data);
    }
}









